// 无依赖的 Actor 系统 - 用于操作系统内核
// 不使用标准库的线程，自己实现调度

/// 内核 Actor 状态
pub const KernelActorState = enum {
    ready,
    running,
    waiting,
    stopped,
};

/// 内核 Actor
pub const KernelActor = struct {
    id: u32,
    state: KernelActorState,
    stack_ptr: usize,
    entry_point: *const fn () void,
    
    // 消息队列（固定大小，避免动态分配）
    mailbox: [16]Message,
    mailbox_head: u8,
    mailbox_tail: u8,
    
    pub fn init(id: u32, entry: *const fn () void, stack: usize) KernelActor {
        return .{
            .id = id,
            .state = .ready,
            .stack_ptr = stack,
            .entry_point = entry,
            .mailbox = undefined,
            .mailbox_head = 0,
            .mailbox_tail = 0,
        };
    }
    
    pub fn send(self: *KernelActor, msg: Message) bool {
        const next_tail = (self.mailbox_tail + 1) % 16;
        if (next_tail == self.mailbox_head) {
            return false; // 队列满
        }
        
        self.mailbox[self.mailbox_tail] = msg;
        self.mailbox_tail = next_tail;
        return true;
    }
    
    pub fn receive(self: *KernelActor) ?Message {
        if (self.mailbox_head == self.mailbox_tail) {
            return null; // 队列空
        }
        
        const msg = self.mailbox[self.mailbox_head];
        self.mailbox_head = (self.mailbox_head + 1) % 16;
        return msg;
    }
};

/// 消息类型
pub const Message = union(enum) {
    interrupt: u8,
    timer_tick,
    custom: u32,
};

/// Actor 调度器
pub const Scheduler = struct {
    actors: [64]KernelActor,
    actor_count: u8,
    current_actor: u8,
    
    pub fn init() Scheduler {
        return .{
            .actors = undefined,
            .actor_count = 0,
            .current_actor = 0,
        };
    }
    
    pub fn spawn(self: *Scheduler, entry: *const fn () void, stack: usize) ?u32 {
        if (self.actor_count >= 64) return null;
        
        const id = self.actor_count;
        self.actors[id] = KernelActor.init(id, entry, stack);
        self.actor_count += 1;
        
        return id;
    }
    
    pub fn schedule(self: *Scheduler) ?*KernelActor {
        // 简单的轮询调度
        var checked: u8 = 0;
        while (checked < self.actor_count) : (checked += 1) {
            self.current_actor = (self.current_actor + 1) % self.actor_count;
            const actor = &self.actors[self.current_actor];
            
            if (actor.state == .ready) {
                actor.state = .running;
                return actor;
            }
        }
        
        return null; // 没有可运行的 Actor
    }
    
    pub fn yield_current(self: *Scheduler) void {
        if (self.current_actor < self.actor_count) {
            self.actors[self.current_actor].state = .ready;
        }
    }
};

/// 全局调度器实例
var global_scheduler: Scheduler = undefined;

pub fn init_scheduler() void {
    global_scheduler = Scheduler.init();
}

pub fn get_scheduler() *Scheduler {
    return &global_scheduler;
}
